package ps.client.gui.util.textpanel;

import java.awt.Color;
import java.awt.Font;


public class DefaultMessageLineFormater implements MessageLineFormater {
    
    public static final Font FONT_TIME_STAMP = new Font("Courier New", Font.PLAIN, 11);
    
    public static final Color COLOR_TIME_STAMP = Color.GRAY;
    
    boolean timeStamp = true;
    
    public TextLine formatMessageLine(String msg, int size, Color color) {
        TextLine textLine = new TextLine();
        
        if (timeStamp) {
//            int stampOffset = size >= 11 ? size - 10 : 0;
            int stampOffset = size == 11 ? 1 : size == 12 ? 2 : size > 12 ? size - 11 : 0;
            textLine.appendText(getCurrentTimeStr(), FONT_TIME_STAMP, COLOR_TIME_STAMP, 0, 3, stampOffset);
        }
        
        Font msgFont = new Font("Arial", Font.PLAIN, size);
        
        textLine.appendText(msg, msgFont, color, 0, 3, 0);

        return textLine;
    }
    
    private String getCurrentTimeStr() {
        long timeSecs = System.currentTimeMillis() / 1000;
        long timeMins = timeSecs / 60;
        long timeHours = timeMins / 60 + 1;
        int hours = (int)(timeHours % 24);
        int mins = (int)(timeMins % 60);
        
        StringBuilder timeStr = new StringBuilder();
        if (hours < 10) {
            timeStr.append('0');
        }
        timeStr.append(hours);
        timeStr.append(':');
        if (mins < 10) {
            timeStr.append('0');
        }
        timeStr.append(mins);
        
        return timeStr.toString();
    }
    
    @Override
    public void setTimeStamp(boolean b) {
        this.timeStamp = b;
    }

    @Override
    public boolean isTimeStamp() {
        return timeStamp;
    }

}
